﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.ServiceHosting.ServiceRuntime;

namespace Dtyo_WebRole.Emp
{
    public partial class EmpDetails : System.Web.UI.Page
    {
        private DtyoDataSource ds;
        private Employee emp;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (ds == null) {
                ds = new DtyoDataSource();
            }

            if (emp == null)
            {
                String s = Request.QueryString["rowkey"];
                emp = ds.SelectEmployee(s);
            }

            if (txtLastName.Text.Length == 0)
            {
                txtLastName.Text = emp.Lastname;
            }
            if (txtFirstName.Text.Length == 0)
            {
                txtFirstName.Text = emp.Firstname;
            }
            if (ddDeps.SelectedIndex == 0)
            {
                ddDeps.SelectedValue = emp.DepartmentKey;
            }
        }

        protected void SaveEmployeeButton_Click(object sender, EventArgs e)
        {
            RoleManager.WriteToLog("Information", string.Format("Saving changes to a employee"));
            /*
            ds = new DtyoDataSource();
            String s = Request.QueryString["rowkey"];
            emp = ds.SelectEmployee(s);
*/
            emp.Firstname = txtFirstName.Text;
            emp.Lastname = txtLastName.Text;
            emp.DepartmentKey = ddDeps.SelectedValue;
            ds.SaveEmployee(emp);
            Response.Redirect("EmpList.aspx");
        }

    }
}